Ext.define("ECAT.view.TopBar", {
    extend: 'Ext.Container',
    requires: ['Ext.Spacer','Ext.Title','ECAT.view.DelFavComfirm','Ext.MessageBox'],
    xtype : 'topbar',
    config : {
    	floatingCls : 'c-x-floating',
    	cls : 'touming',
    	hidden : true,
    	width: '100%',
    	top : 0,
    	layout : 'vbox'
	},
    initialize: function() {
    	this.callParent(arguments);
    	this.setHeight(ECAT.lib.TOP_STRIP_HEIGHT+ECAT.lib.TOP_BAR_HEIGHT);
    	var strip = Ext.create('Ext.Container',{
    		cls : 'topstrip',
    		width: '100%',
        	height: ECAT.lib.TOP_STRIP_HEIGHT,	
        	border : 5
    	}); 
    	this.bar = Ext.create('Ext.Container',{
    		cls : 'toolbar',
    		width: '100%',
        	height: ECAT.lib.TOP_BAR_HEIGHT,	
        	border : 5,
        	layout : 'hbox',
        	items : [{
        		xtype : 'spacer'
        	},{
        		xtype : 'title',
        		id : 'title_tb',
        		centered : true,
        		cls : 'toptitle',
        		width : '40%'
        	},{
        		xtype : 'spacer'
        	}]
		}); 
    	this.add([strip,this.bar]);
    	this.toImgListAll();
    },
    setTitle : function(title){
    	Ext.getCmp('title_tb').updateTitle(title);
    },
    getTitle : function(){
    	return Ext.getCmp('title_tb')._html;
    },
    toImgListItemize : function(title){
    	this.setTitle(title);
    	this.setBtnBack('分类',function(){
    		var list_itemize = Ext.getCmp('list_itemize'),
				topbar = Ext.getCmp('topbar'),
				btn_menu = Ext.getCmp('btn_menu_down');
    		btn_menu.disable();
			topbar.toItemizeList();
			list_itemize.show({type: 'slide',direction: 'left',out: false,duration : 400});
    	});
    	this.setBtnsVisible('listItemize');
    },
    toItemizeList : function(){
    	this.setTitle('分类选择');
    	this.setBtnsVisible('itemize');
    },
    toImgListAll : function(){
    	if(!this.title_list_img_all){
    		var count = Ext.getCmp('list_img_all').store.getCount();
    		this.title_list_img_all = '全部('+count+')';
    	}
    	this.setTitle(this.title_list_img_all);
    	this.setBtnsVisible('listAll');
    },
    toFavorite : function(){
    	var title = '收藏夹',
    		c = Ext.getCmp('list_img_fav').store.getCount();
    	if(c > 0){
    		title += ('('+ c + ')');
    	}
    	this.setTitle(title);
    	
    	if(!this.btn_edit){
    		this.btn_edit = Ext.create('Ext.Button',{
    			id : 'btn_edit',
        		cls : 'black',
        		right : '5%',
        		top : 10,
        		height: 50,
        		width: 100,
        		text : '编辑'
    		});
    		this.bar.add(this.btn_edit);
    	}
    	this.setBtnsVisible('fav_view');
    },
    toFavEdit : function(){
    	var title = '收藏夹编辑';
    	this.setTitle(title);
    	this.setBtnFinish();
    	this.setBtnDel();
    	this.setBtnsVisible('fav_edit');
    },
    toImageDetailView : function(backText, backList, callback, callbackArgs){
		this.setBtnCollect();
		this.setBtnOrder();
    	this.setBtnBack(backText,function(){
	 		Ext.Viewport.animateActiveItem(backList,{type: 'slide', direction: 'left'});
	    	Ext.defer(callback, 1, Ext.getCmp('topbar'), callbackArgs);
		});
   		this.setBtnsVisible('detail');
   	},
    toContactDetailView : function(){
     	this.setTitle('联系方式');
     	this.setBtnsVisible('contact');
     },
     setBtnBack : function(text,fn){
    	var btn = this.btn_back;
    	if(!btn){
    		btn = this.btn_back = Ext.create('Ext.Button',{
        		id : 'btn_back',
        		ui : 'back',
        		style : 'background-image: -webkit-linear-gradient(top, black,#6D6D6D 50%,black);color: white;border: 1px solid black',
        		top : 10,
        		height: 50,
        		width: 90,
        		left : '5%'
    		});
    		this.bar.add(btn);
    	}
    	btn.setText(text);
    	btn.setHandler(fn);
    },
    setBtnFinish : function(){
    	if(!this.btnFinish){
    		this.btnFinish = Ext.create('Ext.Button',{
    			id : 'btn_finish',
        		ui : 'action',
        		top : 10,
        		height: 50,
        		width: 100,
        		right : '5%',
        		text : '完成'
    		});
    		this.bar.add(this.btnFinish);
    	}
    },
    setBtnDel : function(count){
    	var btn = this.btnDelete,
    		text = '删除';
    	if(!btn){
    		btn = this.btnDelete = Ext.create('Ext.Button',{
    			id : 'btn_del',
        		ui : 'decline',
        		left : '5%',
        		top : 10,
        		height: 50,
        		width: 100,
        		text : '删除'
    		});
    		this.bar.add(btn);
    	}
    	if(count || count > 0){
    		btn.setDisabled(false);
    		text += ('('+count+')');
    	}else{
    		btn.setDisabled(true);
    	}
    	btn.setText(text);
    },
   	setBtnCollect : function(){
   		if(!this.btn_collect){
   			this.btn_collect = Ext.create('Ext.Button',{
   			 	id : 'btn_collect',
        		iconCls:'star',
        		iconMask: true,
        		top : 10,
        		height: 50,
        		width: 80,
        		right : '1%',
        		handler : function(){
        			var	imageDetailView = Ext.getCmp('imagedetailview'),
        				store = Ext.getStore('store_fav'),
        				activeModel = imageDetailView.getActiveModel(),
        				className = this.element.dom.className,
        				list_img_fav = Ext.getCmp('list_img_fav');
    				// 已经被收藏
    				if(className.search('collectactive') != -1){
    					var index = store.find('name', activeModel.get('name'));
    					store.removeAt(index);
    					this.removeCls('collectactive');
    					Ext.Msg.alert('操作', '取消收藏成功。', Ext.emptyFn);
    				} else{
    					// 还未被收藏
    					store.add(activeModel);
    					this.addCls('collectactive');
    					Ext.Msg.alert('操作', '收藏成功。', Ext.emptyFn);
    				}
        			store.sync();
        			if(list_img_fav){
        				setTimeout(function(){
        					Ext.getCmp('list_img_fav').refreshList();
        				},10);
        			}
        		}
   			});
   			this.bar.add(this.btn_collect);
   		}
   	},
   	setBtnOrder : function(){
   		if(!this.btn_order){
   			this.btn_order = Ext.create('Ext.Button',{
   			 	id : 'btn_order',
        		cls : 'black',
        		top : 10,
        		height: 50,
        		width: 80,
        		right : '12%',
        		text : '订货',
        		handler : function(){
        			Ext.Msg.prompt('订货', '请输入订货数量:', function(buttonId, value) {
					    if(buttonId == 'ok'){
					    	if(Ext.isEmpty(value) || isNaN(value)){
					    		Ext.Msg.alert('失败','请输入数字。',Ext.emptyFn);
					    	}else{
						    	Ext.Msg.alert('成功','您已订货成功。',Ext.emptyFn);
					    	}
					    }
					});
        		}
   			});
   			this.bar.add(this.btn_order);
   		}
   	},
    setBtnsVisible : function(type){
     	this.setBtnVisible(this.btn_edit,['fav_view'],type);
     	this.setBtnVisible(this.btnDelete,['fav_edit'],type);
     	this.setBtnVisible(this.btnFinish,['fav_edit'],type);
     	this.setBtnVisible(this.btn_back,['detail','listItemize'],type);
     	this.setBtnVisible(this.btn_order,['detail'],type);
     	this.setBtnVisible(this.btn_collect,['detail'],type);
     },
     setBtnVisible : function(btn,visibleTypes,type){
     	if(btn){
     		if(visibleTypes.indexOf(type) > -1){
     			btn.show();
     		}else{
     			btn.hide();
     		}
     	}
     }
});